package com.yuba.front.controller.message;

import com.yuba.common.core.domain.R;
import com.yuba.common.mybatis.core.page.PageQuery;
import com.yuba.common.mybatis.core.page.TableDataInfo;
import com.yuba.common.satoken.utils.LoginHelper;
import com.yuba.common.web.core.BaseController;
import com.yuba.front.domain.vo.FrontMessageVo;
import com.yuba.front.service.IFrontMessageService;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息接口
 */
@RestController()
@RequestMapping("/api/message")
@RequiredArgsConstructor
public class ApiMessageController extends BaseController {

    private final IFrontMessageService frontMessageService;

    /**
     * 获取红点消息列表
     * return List<Long>
     */
    @GetMapping("/redCount")
    public R<List<Long>> redCount() {
        return R.ok(frontMessageService.redCount(LoginHelper.getUserId()));
    }

    /**
     * 消息列表
     *
     * @param pageQuery 分页条件
     * @param type      查询类型
     * @return 列表
     */
    @GetMapping("page/list")
    public R<TableDataInfo<FrontMessageVo>> list(PageQuery pageQuery,
                                                 @RequestParam("type") @Validated @NotBlank(message = "类型不能为空") String type) {
        return R.ok(frontMessageService.pageList(pageQuery, LoginHelper.getUserId(), type));
    }

    /**
     * 标记已读
     * @param ids 消息id
     * @return 列表
     */
    @PostMapping("read")
    public R<Void> read(@RequestBody List<Long> ids) {
        frontMessageService.read(ids);
        return R.ok();
    }


   /**
     * 删除消息
     * @param ids 消息id
     * @return 列表
     */
    @PostMapping("delete")
    public R<Void> delete(@RequestBody List<Long> ids) {
        frontMessageService.deleteWithValidByIds(ids, false);
        return R.ok();
    }


}
